$(function() {
	// 初始化日期选择控件
	$.suredy.datetimepicker.init();

	// 按钮事件（收起）
	$('.children-pay-form-container .btn.cancel').on('click', function() {
		$('.children-pay-modal .children-pay-form-container').addClass('hide');
	});

	// 按钮事件（确定）
	$('.children-pay-form-container .btn.ok').on('click', function() {
		var form = $('.children-pay-form-container form');

		form.ajaxSubmit({
			url : $.suredy.contextPath + '/payment/form/update',
			type : 'post',
			success : function(success, textStatus, jqXHR) {
				if (!success) {
					alert('添加[付款信息]失败！');
					return false;
				}

				var contractId = $('.children-pay-form-container form input[name="contract.id"]').val();
				var listUri = $.suredy.contextPath + '/payment/list/children-contract-pay-list/' + contractId;

				$.suredy.loadContent(listUri + ' .children-pay-list-container>*', '.children-pay-modal .children-pay-list-container', function() {
					// 重绘列表
					$.suredy.init_children_pay_list();

					form.clearForm();

					var count = $('.children-pay-modal .children-pay-list-container .total').text();

					$('span[data-contract-id="' + contractId + '"] span.data').text(count);
				});
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('新建/修改[收款信息]失败！\n' + errorThrown);
			}
		});
	});

	// 初始化收款信息列表
	$.suredy.init_children_pay_list = function() {
		var payList = $('table.children-pay-list');
		var btns = [];

		// create
		if (payList.hasClass('create')) {
			btns[btns.length] = {
				text : '添加',
				icon : 'icon-plus',
				style : 'btn-warning btn-sm',
				click : function(page, pageSize, key) {
					$('.children-pay-modal .children-pay-form-container').removeClass('hide');
				}
			};
		}

		// delete
		if (payList.hasClass('delete')) {
			btns[btns.length] = {
				text : '删除',
				icon : 'icon-trash',
				style : 'btn-danger btn-sm',
				click : function(page, pageSize, key) {
					var checked = $.suredy.list.checked(payList);

					if (checked.length === 0) {
						return false;
					}

					if (!window.confirm('确定删除[' + checked.length + ']条付款信息么？'))
						return false;

					var id = [];
					checked.each(function(i) {
						id[i] = $(this).data('id');
					});

					var uri = $.suredy.contextPath + '/payment/list/delete';

					$.ajax({
						url : uri,
						type : 'post',
						traditional : true,
						data : {
							id : id
						},
						success : function(success, textStatus, jqXHR) {
							if (!success) {
								alert('删除操作失败！');
								return false;
							}

							var contractId = $('.children-pay-form-container form input[name="contract.id"]').val();
							var listUri = $.suredy.contextPath + '/payment/list/children-contract-pay-list/' + contractId;

							$.suredy.loadContent(listUri + ' .children-pay-list-container>*', '.children-pay-modal .children-pay-list-container', function() {
								// 重绘列表
								$.suredy.init_children_pay_list();

								var count = $('.children-pay-modal .children-pay-list-container .total').text();

								$('span[data-contract-id="' + contractId + '"] span.data').text(count);
							});
						},
						error : function(jqXHR, textStatus, errorThrown) {
							alert('删除操作失败！\n' + errorThrown);
						}
					});

				}
			};
		}

		payList.list({
			header : btns.length > 0,
			footer : false,
			checkbox : payList.hasClass('delete'),
			search : false,
			btns : btns
		});
	};

	$.suredy.init_children_pay_list();
});